﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace QuailDashboard.View.Converters
{
    /// <summary>
    /// Converts quail status codes to pretty icons
    /// </summary>
    public class StatusConverter : IValueConverter
    {
        /// <summary>
        /// Modifies the source data before passing it to the target for display in the UI.
        /// </summary>
        /// <returns>
        /// The value to be passed to the target dependency property.
        /// </returns>
        /// <param name="value">The source data being passed to the target.</param>
        /// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the target dependency property.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            /*
             * Abandonded	26
Fail	3497
Information	543338
Pass	2524693
Pending	791
Rollback	1894465
Warning	4346
             */
            if (value is int)
                value = value.ToString();
            
            if (targetType.FullName == "System.Windows.Media.ImageSource" && value is string)
            {
                // icon keys are defined in /Assets/Styles.xaml
                string key;
                switch ((string)value)
                {
                    case "Pass":
                    case "1":
                        key = "IconPass";
                        break;
                    case "Fail":
                    case "2":
                        key = "IconFail";
                        break;
                    case "Warning":
                    case "4":
                        key = "IconWarning";
                        break;
                    case "Rollback":
                    case "10":
                        key = "IconRollback";
                        break;
                    case "Information":
                    case "3":
                        key = "IconInformation";
                        break;
                    case "AsyncPending":
                    case "17":
                        key = "IconAsyncPending";
                        break;
                    case "Pending":
                    case "8":
                        key = "IconPending";
                        break;
                    case "Result":
                    case "9":
                        key = "IconResult";
                        break;
                    case "Startup":
                    case "7":
                        key = "IconStartup";
                        break;
                    case "Navigate":
                    case "5":
                        key = "IconNavigate";
                        break;
                    case "Screenshot":
                    case "6":
                        key = "IconScreenshot";
                        break;
                    case "Abandonded":
                    case "Abandoned":
                    case "11":
                        key = "IconAbandoned";
                        break;
                    case "YSOD":
                    case "12":
                    case "FailYSOD":
                        key = "IconFailYsod";
                        break;
                    case "13":
                    case "Timeout":
                        key = "IconFailTimeout";
                        break;
                    case "14":
                    case "Sync":
                        key = "IconFailSync";
                        break;
                    case "15":
                    case "ThirdParty":
                        key = "IconFailThirdParty";
                        break;
                    case "16":
                    case "Quail":
                        key = "IconFailQuail";
                        break;
                    default:
                        key = "IconOther";
                        break;
                }

                return Application.Current.Resources[key];
            }

            return null;
        }

        /// <summary>
        /// Modifies the target data before passing it to the source object.  This method is called only in <see cref="F:System.Windows.Data.BindingMode.TwoWay"/> bindings.
        /// </summary>
        /// <returns>
        /// The value to be passed to the source object.
        /// </returns>
        /// <param name="value">The target data being passed to the source.</param><param name="targetType">The <see cref="T:System.Type"/> of data expected by the source object.</param><param name="parameter">An optional parameter to be used in the converter logic.</param><param name="culture">The culture of the conversion.</param>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
